package njust.edu.dao.impl;



import njust.edu.dao.TradeItemDao;
import njust.edu.dbutils.SimpleUtils;
import njust.edu.entity.Trade;
import njust.edu.entity.TradeItem;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//import com.fbm.base.BaseDAOImpl;
//
//import com.fbm.dao.TradeItemDao;
//
//import com.fbm.vo.TradeItem;
//import com.fbm.web.DishTypeSale;

public class TradeItemDaoImpl  implements TradeItemDao {
//    static private Integer num =  0;

    @Override
    public TradeItem findById(Long id) {
        String sql = "select id,tradeId,quantity,dishId,dishName,dishPrice from Trade_item where id = ?";
        TradeItem trade_item = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setLong(1, id);
            rs = ps.executeQuery();
            while(rs.next()){
                trade_item  = new TradeItem(rs.getLong(1)
                        ,rs.getLong(2)
                        ,rs.getInt(3)
                        ,rs.getString(4)
                        ,rs.getString(5)
                        ,rs.getFloat(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return trade_item;
    }


    @Override
    public void update(TradeItem trade_item) {
        String sql = "update trade_item set tradeId=?,quantity=?,dishId=?,dishName=?,dishPrice=? where id = ?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setLong(1, trade_item.getTradeId());
            ps.setInt(2,trade_item.getQuantity());
            ps.setString(3,trade_item.getDishId());
            ps.setLong(4,trade_item.getId());
            ps.setString(5,trade_item.getDishName());
            ps.setFloat(6,trade_item.getDishPrice());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return ;

    }

    @Override
    public void delete(Long id) {
        String sql = "delete from trade_item where tradeId = ?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setLong(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return ;

    }

    @Override
    public Long add(TradeItem trade_item) {
        Long id = trade_item.getTradeId()*10000+Integer.parseInt(trade_item.getDishId());
        String sql = "insert into Trade_item(tradeId,quantity,dishId,id,dishName,dishPrice) "
                + "values(?,?,?,?,?,?)";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setLong(1, trade_item.getTradeId());
            ps.setInt(2,trade_item.getQuantity());
            ps.setString(3,trade_item.getDishId());
            ps.setLong(4,id);
            ps.setString(5,trade_item.getDishName());
            ps.setFloat(6,trade_item.getDishPrice());
            ps.executeUpdate();
//            num++;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return id;
    }

    @Override
    public List<TradeItem> getList(Long id) {
        String sql = "select id,tradeId,quantity,dishId,dishName,dishPrice from Trade_item where tradeId = ?";
        List<TradeItem> Tradeitem_list = new ArrayList<>();

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setLong(1, id);
            rs = ps.executeQuery();
            while(rs.next()){
                TradeItem trade_item  = new TradeItem(rs.getLong(1)
                        ,rs.getLong(2)
                        ,rs.getInt(3)
                        ,rs.getString(4)
                        ,rs.getString(5)
                        ,rs.getFloat(6));
                Tradeitem_list.add(trade_item);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return Tradeitem_list;
    }



}
